﻿using System;
using System.Windows.Forms;
using StartupToolsetInterfaces;
using VsxToolset;
using VsxToolset.Core;

namespace MyCompany.StartupToolsetRefactored
{
  public partial class CalculationControl : UserControl
  {
    public CalculationControl()
    {
      InitializeComponent();
    }

    private void CalculationControl_Load(object sender, EventArgs e)
    {
      OperatorCombo.SelectedIndex = 0;
      FirstArgEdit.Text = "0";
    }

    private void CalculateButton_Click(object sender, EventArgs e)
    {
      ICalculationService calcService = 
        GetService(typeof (SCalculationService)) as ICalculationService;
      if (calcService != null)
      {
        string result;
        calcService.Calculate(FirstArgEdit.Text, SecondArgEdit.Text,
                              OperatorCombo.Text, out result);
        ResultEdit.Text = result;
      }
      LogCalculation(FirstArgEdit.Text, SecondArgEdit.Text, OperatorCombo.Text,
        ResultEdit.Text);
      LogCalculationToOutput(FirstArgEdit.Text, SecondArgEdit.Text, OperatorCombo.Text,
        ResultEdit.Text);
    }

    private void LogCalculation(string firstArg, string secondArg, string operation,
      string result)
    {
      string message = String.Format("Calculation executed: {0} {1} {2} = {3}",
        firstArg, operation, secondArg, result);
      ActivityLog.Write(
        result == "#Error" ? ActivityLogType.Error : ActivityLogType.Information,
        "Calculation", message);
    }

    private void LogCalculationToOutput(string firstArg, string secondArg, string operation,
      string result)
    {
      string message = String.Format("Calculation executed: {0} {1} {2} = {3}",
        firstArg, operation, secondArg, result);

      OutputWindowPane pane = OutputWindow.GetPane(typeof (MyDebugPane));
      pane.WriteLine(message);
    }

    [PaneName("My Debug")]
    [InitiallyVisible(true)]
    [ThreadSafe(true)]
    private sealed class MyDebugPane: OutputPaneDefinition
    {}
  }
}
